package com.cpimex.eprice.struts;

import com.cpimex.eprice.Grupo;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import javax.sql.DataSource;
import java.sql.*;
import javax.naming.InitialContext;
import com.cpimex.eprice.usuarios.Usuario;
import javax.servlet.http.HttpSession;
import org.apache.struts.actions.DispatchAction;
import java.util.*;
import com.cpimex.eprice.Parametro;
import com.cpimex.eprice.ClienteCritico;

public class GrupoAction extends DispatchAction {

    /**************************************************************************
     ****************************  Acciones de Grupo **************************
     *************************************************************************/


    public ActionForward listarGrupo(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws
            Exception {

        InitialContext context = new InitialContext();
        DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
        Connection conn = dataSrc.getConnection();
        Statement stmt = conn.createStatement();

        ArrayList arr_grupo = new ArrayList();
        try {
            HttpSession session = request.getSession();
            Usuario usr = (Usuario) session.getAttribute("usuario");



            if (usr == null) {
                if(conn != null && !conn.isClosed())
                    conn.close();
                return mapping.findForward("sesionInvalida");
            }

            String str_nomina = usr.getStr_nomina()!=null?usr.getStr_nomina():"000000";

            Grupo grupo = new Grupo();

            arr_grupo = grupo.listarGrupo(stmt, str_nomina);

            request.setAttribute("arr_grupo", arr_grupo);

            return mapping.findForward("listadogrp");

        } catch (Exception ex){
            throw ex;
        }finally{
            stmt.close();
            conn.close();
        }

    }

    public ActionForward insertarGrupo(ActionMapping mapping,
                                       ActionForm form,
                                       HttpServletRequest request,
                                       HttpServletResponse response) throws
            Exception {

        InitialContext context = new InitialContext();
        DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
        Connection conn = dataSrc.getConnection();
        Statement stmt = conn.createStatement();
        Grupo grupo = new Grupo();
        GrupoForm grupoForm = (GrupoForm) form;


        HttpSession session = request.getSession();
        Usuario usr = (Usuario) session.getAttribute("usuario");

        if (usr == null) {
            if(conn != null && !conn.isClosed())
                conn.close();
            return mapping.findForward("sesionInvalida");
        }

        String str_nomina = usr.getStr_nomina()!=null?usr.getStr_nomina():"000000";

        boolean bool_status;
        String str_descGrupo = grupoForm.getStr_descGrupo();
        String str_lineaGrupo = grupoForm.getStr_lineaGrupo();


        bool_status = grupo.insertarGrupo(conn, str_descGrupo, str_lineaGrupo, str_nomina);

        if(bool_status){
            stmt.close();
            conn.close();
            request.setAttribute("msg", "");
            return mapping.findForward("messageWindow");
        }else{
            stmt.close();
            conn.close();
            request.setAttribute("msg", "");
            return mapping.findForward("messageWindow");
        }
    }


    public ActionForward eliminarGrupo(ActionMapping mapping,
                                       ActionForm form,
                                       HttpServletRequest request,
                                       HttpServletResponse response) throws
            Exception {
        InitialContext context = new InitialContext();
        DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
        Connection conn = dataSrc.getConnection();
        Statement stmt = conn.createStatement();

        Grupo grupo = new Grupo();
        try {
            int id_grupo = Integer.parseInt((String) request.getParameter(
            "id_grupo"));
            grupo.eliminarGrupo(conn, id_grupo);

        } catch (Exception ex) {
            stmt.close();
            conn.close();
            throw ex;
        }finally{
            stmt.close();
            conn.close();
        }
        request.setAttribute("msg", "");
        return mapping.findForward("messageWindow");
    }


    public ActionForward actualizarGrupo(ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response) throws
            Exception {
        InitialContext context = new InitialContext();
        DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
        Connection conn = dataSrc.getConnection();
        Grupo grupo = new Grupo();
        GrupoForm grupoForm = (GrupoForm) form;

        try {
            boolean bool_status;
            String str_idGrupo = grupoForm.getStr_idGrupo();
            String str_descGrupo = grupoForm.getStr_descGrupo();
            String str_lineaGrupo = grupoForm.getStr_lineaGrupo();

            bool_status = grupo.actualizarGrupo(conn, str_idGrupo, str_descGrupo, str_lineaGrupo);

            if(bool_status){
                request.setAttribute("msg", "");
                return mapping.findForward("messageWindow");
            }else{
                request.setAttribute("msg", "");
                return mapping.findForward("messageWindow");
            }
        } catch (Exception ex) {
            throw ex;
        }finally{
            conn.close();
        }
    }

    public ActionForward editarGrupo(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws
            Exception {
        InitialContext context = new InitialContext();
        DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
        Connection conn = dataSrc.getConnection();
        Statement stmt = conn.createStatement();
        GrupoForm grupoForm = (GrupoForm) form;
        ArrayList arr_grupo;
        int int_idGrupo;

        try {
            if (request.getParameter("id_grupo") == null)
                return mapping.findForward("editargrupo");
            else
                int_idGrupo = Integer.parseInt((String) request.getParameter(
                        "id_grupo"));

            Grupo grupo = new Grupo();
            arr_grupo = grupo.muestraGrupo(stmt, int_idGrupo);

            grupoForm.setStr_idGrupo((String)arr_grupo.get(0));
            grupoForm.setStr_descGrupo((String)arr_grupo.get(1));
            grupoForm.setStr_lineaGrupo((String)arr_grupo.get(2));

        }catch (Exception ex) {
            stmt.close();
            conn.close();
            throw ex;
        }finally{
            stmt.close();
            conn.close();
        }
        return mapping.findForward("editargrupo");
    }


    /**************************************************************************
     *************************  Acciones de Parametro *************************
     *************************************************************************/

    public ActionForward muestraParametro(ActionMapping mapping,
                                          ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) throws
            Exception {
        InitialContext context = new InitialContext();
        DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
        Connection conn = dataSrc.getConnection();
        Statement stmt = conn.createStatement();

        try {

            return mapping.findForward("");
        } catch (Exception ex) {
            throw ex;
        } finally {
            stmt.close();
            conn.close();
        }
   }

   public ActionForward editaParametro(ActionMapping mapping,
                                       ActionForm form,
                                       HttpServletRequest request,
                                       HttpServletResponse response) throws
           Exception {
       InitialContext context = new InitialContext();
       DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
       Connection conn = dataSrc.getConnection();
       Statement stmt = conn.createStatement();

       ParametroForm param_form = (ParametroForm) form;
       Parametro param = new Parametro();

       Map map;

       try{
           map = param.muestraParametro(stmt);

           param_form.setStr_idParam((String)map.get("str_idParam"));
           param_form.setStr_avisoExp((String)map.get("str_avisoExp"));
           param_form.setStr_diasExp((String)map.get("str_diasExp"));
           param_form.setStr_directorParam((String)map.get("str_directorParam"));
           param_form.setStr_gerenteParam((String)map.get("str_gerenteParam"));
           param_form.setStr_vpoParam((String)map.get("str_vpoParam"));
           param_form.setStr_estructuraParam((String)map.get("str_estructuraParam"));
           param_form.setStr_dirBackup((String)map.get("str_dirBackup"));
           param_form.setStr_vpoBackup((String)map.get("str_vpoBackup"));

           return mapping.findForward("editaParam");
       }catch(Exception ex){
           throw ex;
       }finally{
           stmt.close();
           conn.close();
       }
   }


   public ActionForward actualizaParametro(ActionMapping mapping,
                                           ActionForm form,
                                           HttpServletRequest request,
                                           HttpServletResponse response) throws
           Exception {
       InitialContext context = new InitialContext();
       DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
       Connection conn = dataSrc.getConnection();
       Statement stmt = conn.createStatement();

       Parametro parametro = new Parametro();
       Map map_param = new HashMap();

       try{
           map_param.put("id_param", request.getParameter("str_idParam"));
           map_param.put("avisoexp_param", request.getParameter("str_avisoExp"));
           map_param.put("diasexp_param", request.getParameter("str_diasExp"));
           map_param.put("director_param", request.getParameter("str_directorParam"));
           map_param.put("gerente_param", request.getParameter("str_gerenteParam"));
           map_param.put("vpo_param", request.getParameter("str_vpoParam"));
           map_param.put("estructura_param", request.getParameter("str_estructuraParam"));
           map_param.put("dirbackup_param", request.getParameter("str_dirBackup"));
           map_param.put("vpobackup_param", request.getParameter("str_vpoBackup"));

           parametro.actualizaParametro(stmt, map_param);
           request.setAttribute("msg", "");
           return mapping.findForward("messageWindow");

       }catch(Exception ex){
           throw ex;
       }finally{
           stmt.close();
           conn.close();
       }
   }


   /**************************************************************************
    **********************  Acciones de Clientes Criticos ********************
    *************************************************************************/


   public ActionForward listadoCliC(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws
            Exception {
        InitialContext context = new InitialContext();
        DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
        Connection conn = dataSrc.getConnection();
        Statement stmt = conn.createStatement();

        try{
            ArrayList arr_cliente = new ArrayList();
            ClienteCritico cliente = new ClienteCritico();
            arr_cliente = cliente.listadoCliC(stmt);

            request.setAttribute("arr_cliente", arr_cliente);

            return mapping.findForward("listadoClienteC");
        }catch(Exception ex){
            throw ex;
        }finally{
            stmt.close();
            conn.close();
        }
    }


    public ActionForward listadoCli(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws
           Exception {
       InitialContext context = new InitialContext();
       DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
       Connection conn = dataSrc.getConnection();
       Statement stmt = conn.createStatement();
       try{
           ClienteCForm clienteform = (ClienteCForm)form;
           String str_search;
           str_search = clienteform.getStr_search();

           if(str_search==null)
               str_search = "";

           ArrayList arr_cliente;
           ClienteCritico cliente = new ClienteCritico();

           arr_cliente = cliente.listadoCli(stmt, str_search);

           request.setAttribute("arr_cliente", arr_cliente);

           return mapping.findForward("listadoCliente");
       }catch(Exception ex){
           throw ex;
       }finally{
           stmt.close();
           conn.close();
       }
   }


   public ActionForward insertaCliC(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws
          Exception {
      InitialContext context = new InitialContext();
      DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
      Connection conn = dataSrc.getConnection();
      Statement stmt = conn.createStatement();

      try{
          String str_idClienteC = request.getParameter("id_clienteC");
          ClienteCritico cliente = new ClienteCritico();

          cliente.insertaClienteC(stmt, str_idClienteC);
          request.setAttribute("msg","");

          return mapping.findForward("insertaCliente");
       }catch(Exception ex){
           throw ex;
       }finally{
           stmt.close();
           conn.close();
       }
   }


   public ActionForward eliminaCliC(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws
            Exception {
        InitialContext context = new InitialContext();
        DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
        Connection conn = dataSrc.getConnection();
        Statement stmt = conn.createStatement();

        try{
            String str_idClienteC = request.getParameter("id_clienteC");
            ClienteCritico cliente = new ClienteCritico();

            cliente.eliminarClienteC(stmt, str_idClienteC);

            return mapping.findForward("eliminaCliente");
        }catch(Exception ex){
            throw ex;
        }finally{
            stmt.close();
            conn.close();
        }
    }
}
